﻿namespace ScrumFactory.Windows
{
    using System.ComponentModel.Composition.Extensions;
    using System.ComponentModel.Composition.Hosting;
    using System.Globalization;
    using System.Windows.Markup;
    using System.Reflection;
    using System.Windows;

    public partial class App : Application
    {
        private ViewModel.IShellViewModel shellViewModel;

        public CompositionContainer Container { get; private set; }

        public App()
        {
            this.Startup += new StartupEventHandler(AppStartup);
        }

        private void AppStartup(object sender, StartupEventArgs e)
        {

            SetFrameworkElementToUserCulture();

            var catalog = new AggregateCatalog();

            catalog.Catalogs.Add(new DirectoryCatalog(System.IO.Directory.GetCurrentDirectory(), "ScrumFactory.*.dll"));
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new ConfigurationCatalog());

            Container = new CompositionContainer(catalog);

            this.shellViewModel = Container.GetExport<ViewModel.IShellViewModel>().Value;
            this.shellViewModel.Show();

        }


        /// <summary>
        /// Sets the Culture to the current user culture.
        /// </summary>        
        private void SetFrameworkElementToUserCulture() {


            FrameworkElement.LanguageProperty.OverrideMetadata(
                typeof(FrameworkElement),
                new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.IetfLanguageTag)));

        }

        public Thickness SystemBorderThickness {
            get {

                double taskBarHeight = SystemParameters.VirtualScreenHeight - SystemParameters.WorkArea.Height + System.Windows.SystemParameters.ResizeFrameHorizontalBorderHeight;                
                return new Thickness(
                    SystemParameters.ResizeFrameVerticalBorderWidth -2,
                    SystemParameters.ResizeFrameHorizontalBorderHeight -1,
                    SystemParameters.ResizeFrameVerticalBorderWidth -3,
                    taskBarHeight -2);

            }
        }
    }
}
